package com.th.apicenter.rta;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.th.apicenter.rta.util.RopHttp;
import com.th.apicenter.tools.ThResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kay
 * @date 2025/6/13
 * @TODO
 */
@RestController
@RequestMapping("/wifirta")
@Slf4j
public class WIFIController {

    /**
     * 接收wifi的rta
     * https://apibiz.tinghongzz.com/wifirta/handle?account_id=59583&oaid=__OAID__&hash_oaid=__HASH_OAID__&imei=__IMEI__&hash_imei=__HASH_IMEI__&idfa=__IDFA__&hash_idfa=__HASH_IDFA__&sid=__SID__&ip=__IP__&ua=__UA__&caid=__CAID__&caid_versiond=__CAID_VERSIOND__&last_caid=__LAST_CAID__&event_type_version=__LAST_CAID_VERSION__&os_version=__OS_VERSION__&model=__MODEL__
     */
    @GetMapping("/handle")
    public ThResult handleWIFIRta(@RequestParam(name = "account_id", required = false) String accountId,
                                  @RequestParam(name = "oaid", required = false) String oaid,
                                  @RequestParam(name = "hash_oaid", required = false) String hash_oaid,
                                  @RequestParam(name = "imei", required = false) String imei,
                                  @RequestParam(name = "hash_imei", required = false) String hash_imei,
                                  @RequestParam(name = "idfa", required = false) String idfa,
                                  @RequestParam(name = "hash_idfa", required = false) String hash_idfa,
                                  @RequestParam(name = "sid", required = false) String sid,
                                  @RequestParam(name = "ip", required = false) String ip,
                                  @RequestParam(name = "ua", required = false) String ua,
                                  @RequestParam(name = "caid", required = false) String caid,
                                  @RequestParam(name = "caid_versiond", required = false) String caid_versiond,
                                  @RequestParam(name = "last_caid", required = false) String last_caid,
                                  @RequestParam(name = "event_type_version", required = false) String event_type_version,
                                  @RequestParam(name = "os_version", required = false) String os_version,
                                  @RequestParam(name = "model", required = false) String model
    ) {
        log.info("account_id:{}", accountId);
        log.info("oaid:{}", oaid);
        log.info("hash_oaid:{}", hash_oaid);
        log.info("imei:{}", imei);
        log.info("hash_imei:{}", hash_imei);
        log.info("idfa:{}", idfa);
        log.info("hash_idfa:{}", hash_idfa);
        log.info("sid:{}", sid);
        log.info("ip:{}", ip);
        log.info("ua:{}", ua);
        log.info("caid:{}", caid);
        log.info("caid_versiond:{}", caid_versiond);
        log.info("last_caid:{}", last_caid);
        log.info("event_type_version:{}", event_type_version);
        log.info("os_version:{}", os_version);
        log.info("model:{}", model);
        return ThResult.success("成功！");
    }


    @GetMapping("/handle01")
    public Map<String,Object> handleRtaRequest(
            @RequestParam(value = "oaid", required = false) String oaid,
            @RequestParam(value = "hash_oaid", required = false) String hash_oaid,
            @RequestParam(value = "imei", required = false) String imei,
            @RequestParam(value = "hash_imei", required = false) String hash_imei,
            @RequestParam(value = "idfa", required = false) String idfa,
            @RequestParam(value = "hash_idfa", required = false) String hash_idfa,
            @RequestParam(value = "caid", required = false) String caid,
            @RequestParam(value = "caid_version", required = false) String v,
            @RequestParam(value = "sid", required = false) String sid,
            @RequestParam(value = "ip", required = false) String ip,
            @RequestParam(value = "user_agent", required = false) String user_agent,
            @RequestParam(value = "os_version", required = false) String os_version,
            @RequestParam(value = "model", required = false) String model,
            @RequestParam ( required = false)Map<String, String> customParams) throws IOException {
        Map<String, Object> param = new HashMap<>(RopHttp.params);

        RopHttp.setParameter(param,"method","taobao.tbk.dg.newuser.order.get");
        RopHttp.setParameter(param,"adzone_id","116087600242");
        RopHttp.setParameter(param,"page_size","20");
        RopHttp.setParameter(param,"page_no","1");
        RopHttp.setParameter(param,"start_time","2025-07-11 00:34:05");
        RopHttp.setParameter(param,"end_time","2025-07-21 23:59:59");
        RopHttp.setParameter(param,"activity_id","0");
        //签名获取
        String sign = RopHttp.signTopRequest(param, "md5");
        RopHttp.setParameter(param,"sign",sign);


        String post = HttpUtil.get("http://gw.api.taobao.com/router/rest", param, 5000);

        Map<String,Object> map=new HashMap<>();
        map.put("result", JSONObject.parseObject(post));

        return map;
    }

}
